//
//  ZLTitlebutton.swift
//  ZLWeibo
//
//  Created by zhanglongtao on 16/12/26.
//  Copyright © 2016年 hanju001. All rights reserved.
//

import UIKit

class ZLTitlebutton: UIButton {

    //tiltle如果是nil就显示首页  否则显示name和图片
    init(title: String?) {
        
        super.init(frame: CGRect())
        
        if (title == nil){
            setTitle("首页", for: .normal)
        }
        else{
            setTitle(title! + " ", for: .normal)
            
            setImage(UIImage(named: "navigationbar_arrow_down"), for: .normal)
            setImage(UIImage(named: "navigationbar_arrow_up"), for: .selected)
        }
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        setTitleColor(UIColor.darkGray, for: .normal)

        sizeToFit()
        
        layoutSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //重新布局子视图
    override func layoutSubviews() {
        super.layoutSubviews()
        
        guard let titleLabel = titleLabel, let imageView = imageView else {
            
            return
        }
        
        titleLabel.frame = titleLabel.frame.offsetBy(dx: -imageView.bounds.width, dy: 0)
        imageView.frame = imageView.frame.offsetBy(dx: titleLabel.bounds.width, dy: 0)
        
    }
}
